(function() {

    var ChangePassword = View.extend(ChangePassword = {
        
        init: function() {
            this.bindUI();
        },
        
        bindUI: function() {
            var self = this;
            
            $("#change_pass_form").submit(function(e) {
                e.preventDefault();
                
                self.changePass();
            });
            
            $("#change_pass_form #cancel_btn").click(function() {
                window.location.href = $("#dashboard_url").val();
            });
        },
        
        changePass: function() {

            var url = $("#change_pass_form").attr('action');
            var params = $("#change_pass_form").serialize();

            $.ajax({
                url: url,
                data: params,
                type: 'post',
                dataType: 'json',
                success: function( res ) {
                    if ( res.status == 'failed' ) {
                        alert( res.message );
                    }
                    
                    else if( res.status == 'success' ) {
                        alert( res.message );
                        window.location.href = $("#dashboard_url").val();
                    }
                },
                error: function( xhr ) {
                    console.log(xhr.responseText);
                    alert("Process failed due to server side error. Please try again");
                }
            });
        }
    });

    $(document).ready(function() {
        var pass = new ChangePassword();
    });
})();

